#include<math.h>
int prime(int x)
{
    if (x < 2)
        return 0;
    for (int i = 2; i <= x / i; i++)
        if (x % i == 0)
            return 0;
    return 1;
}

int snake_demon(int n)
{
    int t = n;
    int p = 0;
    while (t > 0)
    {
        p++;
        t /= 10;
    }
    while (n > 0)
    {
        if ((!prime(n)) || n < t / 10)
            return 0;
        p--;
        t = (int)pow(10, p);
        n %= t;
    }
    return 1;
}